import 'package:adaptive_dialog/adaptive_dialog.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

import 'package:get/get.dart';
import 'package:sainbainu/common/enums/gender.dart';

import 'package:sainbainu/common/widgets/cancel_confirm_wrapper.dart';
import 'package:modal_bottom_sheet/modal_bottom_sheet.dart';
import 'package:nested_scroll_view_plus/nested_scroll_view_plus.dart';

import 'index.dart';
import 'widgets/widgets.dart';

class UserInfoPage extends GetView<UserInfoController> {
  const UserInfoPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<UserInfoController>(
      builder: (_) {
        return Scaffold(
          body: _buildNestedScrollView(),
        );
      },
    );
  }

  Widget _buildNestedScrollView() {
    return NestedScrollViewPlus(
      controller: controller.state.scrollController,
      overscrollBehavior: OverscrollBehavior.outer,
      headerSliverBuilder: (context, innerScrolled) {
        return [
          CustomSliverHeader(
            onBackTap: () => Get.back(),
            onCoverEditTap: () => controller.onEditCover(),
            onAvatarEditTap: () => controller.onEditAvatar(),
          ),
        ];
      },
      body: SingleChildScrollView(
        child: Padding(
          padding: const EdgeInsets.all(15.0),
          child: ClipRRect(
            borderRadius: const BorderRadius.all(Radius.circular(10)),
            clipBehavior: Clip.antiAlias,
            child: _buildForm(),
          ),
        ),
      ),
    );
  }

  Widget _buildForm() {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        _buildIdGetBuilder(
          'nickName',
          (_) => CustomCell(
            title: '昵称'.tr,
            value: _.state.userProfileResponse.nickName,
            placeholder: '${'设置'.tr}${'昵称'.tr}',
            onTap: showNickNameDialog,
          ),
        ),
        _buildIdGetBuilder(
          'intro',
          (_) => CustomCell(
            title: '简介'.tr,
            value: _.state.userProfileResponse.intro,
            placeholder: '${'设置'.tr}${'简介'.tr}',
            onTap: showIntroDialog,
          ),
        ),
        _buildIdGetBuilder(
          'gender',
          (_) => CustomCell(
            title: '性别'.tr,
            value: Gender.label(_.state.userProfileResponse.gender),
            placeholder: '${'设置'.tr}${'性别'.tr}',
            onTap: () => showSexPicker(),
          ),
        ),
        _buildIdGetBuilder(
          'birthday',
          (_) => CustomCell(
            title: '生日'.tr,
            value: _.state.userProfileResponse.birthday,
            placeholder: '${'选择'.tr}${'生日'.tr}',
            onTap: () => showBirthdayPicker(),
          ),
        ),
        _buildIdGetBuilder(
          'address',
          (_) => CustomCell(
            title: '所在地'.tr,
            value: _.state.userProfileResponse.seatAddress,
            placeholder: '${'选择'.tr}${'所在地'.tr}',
            onTap: () => showRegionPicker(),
          ),
        ),
        _buildIdGetBuilder(
          'uniqueNo',
          (_) => CustomCell(
            title: 'SNID'.tr,
            value: _.state.userProfileResponse.uniqueNo,
            placeholder: '${'设置'.tr}SNID',
            onTap: showUniqueNoDialog,
          ),
        ),
        _buildIdGetBuilder(
          'job',
          (_) => CustomCell(
            title: '职业'.tr,
            value: _.state.userProfileResponse.job,
            placeholder: '${'设置'.tr}${'职业'.tr}',
            hideDivider: true,
            onTap: showJobDialog,
          ),
        ),
      ],
    );
  }

  Widget _buildIdGetBuilder(
    String id,
    Widget Function(UserInfoController) builder,
  ) {
    return GetBuilder<UserInfoController>(id: id, builder: builder);
  }

  /// 显示昵称弹窗
  void showNickNameDialog() {
    showBarModalBottomSheet(
      context: Get.context!,
      barrierColor: Colors.black54,
      builder: (context) => NickNameDialog(
        value: controller.state.userProfileResponse.nickName,
        onCancelTap: controller.onBack,
        onConfirmTap: (String value) => controller.onNickNameUpdate(value),
      ),
    );
  }

  /// 显示简介弹窗
  void showIntroDialog() {
    showBarModalBottomSheet(
      context: Get.context!,
      barrierColor: Colors.black54,
      builder: (context) => IntroDialog(
        value: controller.state.userProfileResponse.intro,
        onCancelTap: controller.onBack,
        onConfirmTap: (String value) => controller.onIntroUpdate(value),
      ),
    );
  }

  /// 显示唯一号弹窗
  void showUniqueNoDialog() {
    showBarModalBottomSheet(
      context: Get.context!,
      barrierColor: Colors.black54,
      builder: (context) => UniqueNoDialog(
        value: controller.state.userProfileResponse.uniqueNo,
        onCancelTap: controller.onBack,
        onConfirmTap: (String value) => controller.onUniqueNoUpdate(value),
      ),
    );
  }

  /// 显示职位弹窗
  void showJobDialog() {
    showBarModalBottomSheet(
      context: Get.context!,
      barrierColor: Colors.black54,
      builder: (context) => JobDialog(
        value: controller.state.userProfileResponse.job,
        onCancelTap: controller.onBack,
        onConfirmTap: (String value) => controller.onJobUpdate(value),
      ),
    );
  }

  /// 显示性别选择器
  Future<void> showSexPicker() async {
    int? genderValue = await showConfirmationDialog<int>(
      context: Get.context!,
      title: '${'选择'.tr}${'性别'.tr}',
      style: AdaptiveStyle.iOS,
      actions: [
        AlertDialogAction(
          key: Gender.male,
          label: Gender.label(Gender.male)!.tr,
        ),
        AlertDialogAction(
          key: Gender.female,
          label: Gender.label(Gender.female)!.tr,
          isDestructiveAction: true,
        ),
      ],
    );
    if (genderValue != null) {
      controller.handleUpdateGender(genderValue);
    }
  }

  /// 显示生日选择器
  void showBirthdayPicker() {
    showBarModalBottomSheet(
      context: Get.context!,
      barrierColor: Colors.black54,
      builder: (context) => SizedBox(
        height: 400,
        child: CancelConfirmWrapper(
          titleText: '${'选择'.tr}${'生日'.tr}',
          onCancelTap: controller.onBack,
          onConfirmTap: controller.onBirthPickerConfirm,
          child: CupertinoDatePicker(
            mode: CupertinoDatePickerMode.date,
            initialDateTime: controller.strBirthdayToDate(
              controller.state.userProfileResponse.birthday,
            ),
            minimumDate: controller.state.birthMinDate,
            maximumDate: controller.state.birthMaxDate,
            onDateTimeChanged: controller.onBirthPickerChange,
          ),
        ),
      ),
    );
  }

  /// 显示地区选择器弹窗
  showRegionPicker() {
    showBarModalBottomSheet(
      context: Get.context!,
      barrierColor: Colors.black54,
      builder: (context) => SizedBox(
        height: 400,
        child: RegionPickerDialog(
          onCancelTap: controller.onBack,
          onConfirmTap: controller.onAddressUpdate,
        ),
      ),
    );
  }
}
